package com.jindy.module.drawBill.controller;

import com.jindy.common.annotation.Log;
import com.jindy.common.core.controller.BaseController;
import com.jindy.common.core.domain.AjaxResult;
import com.jindy.common.core.page.TableDataInfo;
import com.jindy.common.enums.BusinessType;
import com.jindy.common.utils.poi.ExcelUtil;
import com.jindy.module.drawBill.domain.DrawBill;
import com.jindy.module.drawBill.domain.DrawBillSearch;
import com.jindy.module.drawBill.service.IDrawBillService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 开票申请Controller
 * 
 * @author yu
 * @date 2023-05-17
 */
@RestController
@RequestMapping("/module/drawBill")
public class DrawBillController extends BaseController
{
    private static final Logger LOGGER = LoggerFactory.getLogger(DrawBillController.class);

    @Autowired
    private IDrawBillService drawBillService;

    /**
     * 查询开票申请列表
     */
    @PreAuthorize("@ss.hasPermi('module:drawBill:list')")
    @GetMapping("/list")
    public TableDataInfo list(DrawBill drawBill)
    {
        startPage();
        List<DrawBill> list = drawBillService.selectDrawBillList(drawBill);
        return getDataTable(list);
    }

    /**
     * 导出开票申请列表
     */
    @PreAuthorize("@ss.hasPermi('module:drawBill:export')")
    @Log(title = "开票申请", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, DrawBill drawBill)
    {
        List<DrawBill> list = drawBillService.selectDrawBillList(drawBill);
        ExcelUtil<DrawBill> util = new ExcelUtil<DrawBill>(DrawBill.class);
        util.exportExcel(response, list, "开票申请数据");
    }

    /**
     * 获取开票申请详细信息
     */
    @PreAuthorize("@ss.hasPermi('module:drawBill:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(drawBillService.selectDrawBillById(id));
    }

    /**
     * 新增开票申请
     */
    @PreAuthorize("@ss.hasPermi('module:drawBill:add')")
    @Log(title = "开票申请", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody DrawBill drawBill)
    {
        try {
            drawBillService.insertDrawBill(drawBill);
            return AjaxResult.success();
        } catch (Exception e) {
            LOGGER.error("新增开票申请失败",e);
            return AjaxResult.error(e.getMessage());
        }
    }

    /**
     * 修改开票申请
     */
    @PreAuthorize("@ss.hasPermi('module:drawBill:edit')")
    @Log(title = "开票申请", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody DrawBill drawBill)
    {
        try {
            return toAjax(drawBillService.updateDrawBill(drawBill));
        } catch (Exception e) {
            LOGGER.error("修改开票申请失败",e);
            return AjaxResult.error(e.getMessage());
        }
    }

    /**
     * 删除开票申请
     */
    @PreAuthorize("@ss.hasPermi('module:drawBill:remove')")
    @Log(title = "开票申请", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(drawBillService.deleteDrawBillByIds(ids));
    }

    /**
     * 获取开票申请信息
     * @param drawBillSearch
     * @return
     */
    @PostMapping("/getSettlement")
    public AjaxResult getSettlement(@RequestBody DrawBillSearch drawBillSearch)
    {
        try {
            return drawBillService.getSettlement(drawBillSearch);
        } catch (Exception e) {
            LOGGER.error("获取开票申请信息失败",e);
            return AjaxResult.error(e.getMessage());
        }
    }

    /**
     * 获取可结算余额
     * @param serialNum
     * @return
     */
    @GetMapping("/getSettlement/{serialNum}")
    public AjaxResult getAblePayAmount(@PathVariable String serialNum)
    {
        try {
            return drawBillService.getAblePayAmount(serialNum);
        } catch (Exception e) {
            LOGGER.error("获取开票申请信息失败",e);
            return AjaxResult.error(e.getMessage());
        }
    }
}
